$(document).ready(function() { 

	/*** Initialisations ***/
	Menu.init();
	Category.init();
	Search.init();
	Tabs.init();
	Cart.init();
	Product.init();
	
	$('.payment_module').hide();
	$('.paiment_be2bill').show();

	$('#show_payment_module').click(function() {
		$('.payment_module').show();
		$(this).hide();
	});
	
	$(".pop_liv_gra").hover(
		function () {
			$('.pop_liv_gra').click(function() {
			$.colorbox({
				href: '#livraison_gratuite',
				inline: true
			});
		});
	  }
	);
	
	$(".pop_expe_expr").hover(
		function () {
			$('.pop_expe_expr').click(function() {
			$.colorbox({
				href: '#expedition_express',
				inline: true
			});
		});
	  }
	);
	
	$(".pop_retour_off").hover(
		function () {
			$('.pop_retour_off').click(function() {
			$.colorbox({
				href: '#retour_offert',
				inline: true
			});
		});
	  }
	);
	
	$('.pop_referral').click(function() {
		$.colorbox({
			href: '#pop_referral',
			inline: true
		});
	});
	
	$('#maxishow').click(function() {
		$.colorbox({
			href: '#paiement_maxicheque',
			inline: true
		});
	});	
	$('#maxipayment').click(function() {
		$.colorbox({
			href: '#resum_maxicheque',
			inline: true
		});
	});	 	

}); 


/***
 * Menu
 ***/
var Menu = {
	item: false,
	container: false,
	visible: false,
	
	init: function() {
		$(".menu_container").hide(); 
		$(".menu_item").hover(function(e) {
				Menu.show($(e.currentTarget));
			}, function() { 
			Menu.visible = false;
			Menu.hide();
		});
		$('.menu_container').hover(function() {
			Menu.visible = true;
			Menu.show($('#show_' + $(this).attr('id')));
		}, function() {
			Menu.visible = false;
			Menu.hide();
		});
		
		$('#social a').hover(function() {
			var img = $('img', $(this));
			img.attr('src', img.attr('src').replace('-nb', ''));
		}, function() {
			var img = $('img', $(this));
			img.attr('src', img.attr('src').replace('.png', '-nb.png'));
		});
	},
	show: function(elem) {
		Menu.item = elem.parents('li');
		Menu.container = $('#' + elem.attr('id').substr(5));
		Menu.container.show();
		Menu.item.find('a').addClass('here');
		Menu.visible = true;
	},
	hide: function() {
		if(!Menu.visible) {
			Menu.item.find('a').removeClass('here');
			Menu.container.hide();
		}
	}
};

/*** 
* Catégorie
***/
function replace(chaine){
	for ( var i=1; i<=10; i++){ 
		chaine = chaine.replace('&nbsp;', '');
		chaine = chaine.replace(' ', '');	
		chaine = chaine.replace('-', '');	
		chaine = chaine.replace('(', '');	
		chaine = chaine.replace(')', '');
		chaine = chaine.replace('/', '');
		chaine = chaine.replace('é', 'e');
		chaine = chaine.replace('è', 'e');
		chaine = chaine.replace('ê', 'e');
		chaine = chaine.replace('ë', 'e');
		chaine = chaine.replace('à', 'a');
		chaine = chaine.replace('ç', 'c');
		chaine = chaine.replace(',', '');
		chaine = chaine.replace(';', '');
		chaine = chaine.replace('\'', '');
	}
	return chaine;
}
var Category = {
	init: function() {
		/* Hover */
		$('#content .product').hover(function() {
			$('.hover_product', $(this)).css('display', 'block');
			/*$(this).addClass('hover');
			if($('.second_pict', $(this)).length > 0) {
				$('.first_pict', $(this)).addClass('hide');
				$('.second_pict', $(this)).removeClass('hide');
			}*/
		}, function() {
			$('.hover_product', $(this)).css('display', 'none');
			/*$(this).removeClass('hover');
			if($('.second_pict', $(this)).length > 0) {
				$('.second_pict', $(this)).addClass('hide');
				$('.first_pict', $(this)).removeClass('hide');
			}*/
		});
		
		/* Back */
		$('.back').live('click', function() {
			window.history('-1');
		});
		
		//Décocher categorie autre que petit prix
		$('p#open_40 > input:[name=search_cat], p.child40 > input:[name=search_cat]').change(function(){
			$('p:not(#open_40):not(.child40) > input:[name=search_cat]').attr('checked', false);	
		});
		$('p:not(#open_40):not(.child40) > input:[name=search_cat]').change(function(){
			$('p#open_40 input:[name=search_cat], p.child40 input:[name=search_cat]').attr('checked', false);	
		});
		
		//Ouverture des catégories cochées
		$('p.enfant input:checked[name=search_cat]').each(function(){
			monEnfant = $(this).parent().attr('class');
			monEnfant = monEnfant.split(' ');

			if (monEnfant[1]){
				mesOpen = monEnfant[1].replace('child', '');
				if (mesOpen){
					t = $('p.child'+mesOpen);	
					ts = $('#open_' +mesOpen+ ' span');
					t.addClass('opened');
					$(t).show();				
					ts.removeClass('backOpenCat');
					ts.addClass('backCloseCat');
				}
			}
		});
		$('p.ouvreCat input:checked[name=search_cat]').each(function() {
			maMere = $(this).parent().attr('id');
			if (maMere){
				mesOpen = maMere.replace('open_', '');
				if (mesOpen){
					t = $('p.child'+mesOpen);	
					ts = $('#open_' +mesOpen+ ' span');
					t.addClass('opened');
					$(t).show();				
					ts.removeClass('backOpenCat');
					ts.addClass('backCloseCat');
				}
			}
		});
		
		/*Cacher catégories*/
		$('.ouvreCat').click(function() {
			var m = $(this).attr('id');
			ma = m.replace('open_', '');
			t = $('.child'+ma);
			ts = $('#' +m+ ' span');
			if(t.is('.opened')) {
				t.removeClass('opened');
				$(t).hide();
				ts.addClass('backOpenCat');
				ts.removeClass('backCloseCat');
				
			} 
			else {
				t.addClass('opened');
				$(t).show();				
				ts.removeClass('backOpenCat');
				ts.addClass('backCloseCat');
				
			}
		});
		
		/*DECocher categories fille auto*/
			$('p.ouvreCat').click(function() {			
				maMere = $(this).attr('id');
				if (maMere){
					mesOpen = maMere.replace('open_', '');
					if (mesOpen){
						$('p.child'+mesOpen+' input:[name=search_cat]').attr('checked', false);	
					}
					$(this).submit();
					// return false;
				}
			});
			/*DECocher categories mère auto*/
			$('p.enfant').click(function() {
				monEnfant = $(this).attr('class');
				monEnfant = monEnfant.split(' ');
				
				if (monEnfant[1]){
					mesOpen = monEnfant[1].replace('child', '');
					if (mesOpen){
						$('p#open_'+mesOpen+' input:[name=search_cat]').attr('checked', false);	
					}					
					$(this).submit();
					// return false;
				}
			});
					
		/* Filtres */
		$('#search_filter').change(function() {					
			
			/*DECocher categories fille auto*/
			$('p.ouvreCat').click(function() {			
				maMere = $(this).attr('id');
				if (maMere){
					mesOpen = maMere.replace('open_', '');
					if (mesOpen){
						$('p.child'+mesOpen+' input:[name=search_cat]').attr('checked', false);	
					}
					$(this).submit();
					// return false;
				}
			});
			/*DECocher categories mère auto*/
			$('p.enfant').click(function() {
				monEnfant = $(this).attr('class');
				monEnfant = monEnfant.split(' ');
				
				if (monEnfant[1]){
					mesOpen = monEnfant[1].replace('child', '');
					if (mesOpen){
						$('p#open_'+mesOpen+' input:[name=search_cat]').attr('checked', false);	
					}					
					$(this).submit();
					// return false;
				}
			});
			
			$(this).submit();
			
		});
		$('#search_filter').submit(function() {
			
			// var catname = $('input:checked[name=search_cat]').text();			
			// var manname = $('input:checked[name=search_brand]').text();			
			// var siname = $('input:checked[name=search_size]').text();					
			// var coname = $('input:checked[name=search_color]').text();			
			
		
			// var brand = $('#search_brand option:selected[value]');
			var brand = "";
			var manname = "";
			$('input:checked[name=search_brand]').each(function() {
				temp_brand = ($(this).val()).split(';');
				if (temp_brand[0] && temp_brand[1]){
					if (brand==""){
						brand += temp_brand[0];
						manname += temp_brand[1];
					}
					else {
						brand += ',' + temp_brand[0];
						manname += ',' + temp_brand[1];
					}
				}				
			});

			// var size = $('#search_size option:selected[value]');
			var size = "";
			var siname ="";
			$('input:checked[name=search_size]').each(function() {
				temp_size = ($(this).val()).split(';');
				if (temp_size[0] && temp_size[1]){
					if (size==""){
						size += temp_size[0];
						siname += temp_size[1];
					}
					else {
						size += ',' + temp_size[0];
						siname += ',' + temp_size[1];
					}
				}
			});
			
			// var color = $('#search_color option:selected[value]');
			var color = "";
			var coname = "";
			$('input:checked[name=search_color]').each(function() {
				temp_color = ($(this).val()).split(';');
				if (temp_color[0] && temp_color[1]){
					if (color==""){
						color += temp_color[0];
						coname += temp_color[1];
					}
					else {
						color += ',' + temp_color[0];
						coname += ',' + temp_color[1];
					}
				}
			});
			
			// var cat = $('#search_cat option:selected[value]');
			var cat = "";
			var catname = "";
			$('input:checked[name=search_cat]').each(function() {
				temp_cat = ($(this).val()).split(';');
				if (temp_cat[0] && temp_cat[1]){
					if (cat==""){
						cat += temp_cat[0];
						catname += temp_cat[1];
					}
					else {
						cat += ',' + temp_cat[0];
						catname += ',' + temp_cat[1];
					}
				}
			});
			
			catname = replace(catname);
			manname = replace(manname);
			siname = replace(siname);
			coname = replace(coname);
			
			if (catname.length>30) catname = "lingerie-multi-categories";
			if (manname.length>30) manname = "lingerie-multi-marques";
			if (siname.length>30) siname = "multi_taille";
			if (coname.length>30) coname = "multi_couleur";
			
			var price_min = $('#search_price_min').val();
			var price_max = $('#search_price_max').val();
			var query = $('#search_query').val();
			
			var url = $(this).attr('action') + '_';
			if(query) {
				url += query;
			}
			else {
				url +='';
			}
			
			url += '-';
			
			if(price_min.length > 0) {
				url += price_min + '-';
			}
			else {
				url += '-';
			}
			if(price_max.length > 0) {
				url += price_max + '-';
			}
			else {
				url += '-'				
			}

			if(cat.length > 0) {
				url += cat + '_' + catname + '-';
			}
			else {
				url += '' + '_-';
			}
			
			if(brand.length > 0) {
				url += brand + '_' + manname + '-';
			}
			else {
				url += '' + '_-';
			}
			if(size.length > 0) {
				url += size + '-';
			}
			else {
				url += '-';
			}
			if(color.length > 0) {
				url += color + '-';
			}
			else {
				url += '-';
			}
			
			if(size.length > 0) {
				url +=  siname + '_';
			}
			else {
				url += '_';
			}
			
			if(color.length > 0) {
				url +=  coname;
			}
			else {
				url += '';
			}
			
			url += '.html';
	
			// alert(url);
			window.location.href = url;
			return false;
		});
		
		/* Nav */
		$('.back').click(function() {
			window.history.back();
			return false;
		});
		
		/* Popins */
		$('.colorbox').colorbox({
			iframe: true,
			width:'942px',
			height:'866px'
		});
		
		$('.group').colorbox({
			rel: 'group'
		});
	
	}, 
	resetFilter: function(elem) {
		$('#search_filter #' + elem).val('');
		$('#search_filter').submit();
	}
};

/***
* Search
***/
var Search = {
	init: function() {
		$('#search input').focus(function() {
			if($(this).is('.initial')) {
				$(this).val('');
				$(this).removeClass('initial');
			}
		});
		
		$('#email_nl').focus(function() {
			if($(this).is('.initial')) {
				$(this).val('');
				$(this).removeClass('initial');
			}
		});
		$('#submit-newsletter').click(function() {
			$('#form_newsletter').submit();
		});
	}
};

/*** 
* Tabs 
***/
var Tabs = {
	init: function() {
		$('.tabs li a').click(function() {
			return Tabs.changeTab($(this));
		});
	},
	changeTab: function(elem) {
		var anchor = elem.attr('href').substr(1);
		$('.tabs li').removeClass('selected');
		elem.parent('li').addClass('selected');
		$('.tab-content').removeClass('active');
		$('#' + anchor + '-content').addClass('active');
		return false;
	}
};

/***
* Cart 
***/
var Cart = {
	visible: false,
	
	init: function() {
		$('#header-cart').hover(function() {
			if(!Cart.visible) {
				$('#cart').addClass('selected');
				$('#cartBackground', $(this)).slideDown('fast') ;
				$('#cart-content', $(this)).slideDown('fast', function() { 
					Cart.visible = true; 
				});
			}
		}, function() {
		
			$('#cartBackground', $(this)).slideUp('fast');
			$('#cart-content',  $(this)).slideUp('fast', function() { 
				Cart.visible = false; 
				
				$('#cart').removeClass('selected');
			});
		});
	}
};

/***
 * Product
 ***/
var Product = {
	init: function() {
		$('.selectSize').click(function() {
			if($(this).is('.nostock')) {
				$('#product_addcart').hide();
				$('#sizenotfound-size').html($(this).html());
				$('#size-attribute_id').val($(this).attr('id'));
				$.colorbox({
					href: '#sizenotfound',
					inline: true
				});
			} else {
				$('#product_addcart').show();
			}
		});
		
		$('#stock-submit').live('click', function() {
			var name = $('#stock-name').val();
			var mail = $('#stock-mail').val();
			var idproduct = $('#size-product_id').val();
			var idattribute = $('#size-attribute_id').val();
			if(name && mail && idattribute!=0 ) {
				$.ajax({
					url: window.location.href,
					data: {
						sizemail: mail, 
						sizename:name,
						id_product: idproduct,
						id_attribute: idattribute
					},
					type: 'POST', 
					success: function(data) {
						$.colorbox.close();
					}
				})
			} else {
				$('#stock-errors').css('visibility', 'visible');
			}
		});
		$('.cb-close').live('click', function() {
			$.colorbox.close();
		});
		
		$('.jqzoom').bind("contextmenu",function(e){
			return false;
		});
	
	}
	
};

$('#continuer-submit').live('click', function() {
	$.colorbox.close();
});

$('.text-manufac-preplus #deroule-man').live('click', function() {
	$('.text-manufac-plus').css('display', 'block');
	$(this).css('display', 'none');
});

/***
 * Cache
 */
var Cache = {
  cache: [],
  preLoadImages: function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      Cache.cache.push(cacheImage);
    }
  }
};